/**
 * 杂项
 * */
package org.zmhhxl.common.core.utils;

import org.apache.commons.text.similarity.LevenshteinDistance;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

public class GeneralFunction {

   /**
    * 随机生成指定长度的字符串
    * */
   public static String getRandomString(int length) {
      String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSObjectUVWXYZ0123456789";
      StringBuilder sb = new StringBuilder();
      for (int i = 0; i < length; i++) {
         int number = (int) (Math.random() * 62);
         sb.append(str.charAt(number));
      }
      return sb.toString();
   }

   /**
    * 从URL中提取域名
    * */
   public static String extractDomain(String url) {
      // 移除协议部分
      int protocolEnd = url.indexOf("://");
      if (protocolEnd != -1) {
         url = url.substring(protocolEnd + 3);
      }

      // 移除路径部分
      int pathStart = url.indexOf("/");
      if (pathStart != -1) {
         url = url.substring(0, pathStart);
      }
      return url;
   }

   /**
    * 模糊匹配map中的键和值,利用FuzzyWuzzy库的 LevenshteinDistance算法
    * */
   public static Map<String, Object> mapFuzzyMatch(Map<String, Object> mb, String searchKey, double similarityObjecthreshold) {
      Map<String, Object> map = new HashMap<>();

      // 模糊匹配键
      for (Map.Entry<String, Object> entry : mb.entrySet()) {
         double similarity = new LevenshteinDistance().apply(searchKey, entry.getKey());
        if (similarity >= similarityObjecthreshold) {
            map.put(entry.getKey(), entry.getValue());
        }
      }
      return map;
   }

   /**
    *             URL parsedUrl = new URL(url);
    *             String protocol = parsedUrl.getProtocol();
    *
    *              // 正则表达式匹配协议部分
    *         Pattern pattern = Pattern.compile("^(http?|https?|ftp)://");
    *         Matcher matcher = pattern.matcher(url);
    *
    *         if (matcher.find()) {
    *             String protocol = matcher.group(1);
    *             System.out.println("Protocol: " + protocol);
    *         } else {
    *             System.err.println("No protocol found in URL: " + url);
    *         }
    *
    * "al.*"表示匹配以"al"开头的字符串，".*al"表示匹配以"al"结尾的字符串，".*al.*"表示匹配包含"al"的字符串。
    * 模糊匹配map中的键,利用正则表达式匹配
    * */
   public static Map<String, Object> mapFuzzyPatternMatch(Map<String, Object> mb, String searchKey) {
      Map<String, Object> map = new HashMap<String, Object>();

      Pattern pattern = Pattern.compile(searchKey);

      // 模糊匹配键
      for (Map.Entry<String, Object> entry : mb.entrySet()) {
         if (pattern.matcher(entry.getKey()).find()) {
            map.put(entry.getKey(), entry.getValue());
         }
      }
      return map;
   }

   /**
    * 获取URL的协议部分
    * */
   public static String extractUrlProtocol(String url) {
      URL parsedUrl = null;
      try {
         parsedUrl = new URL(url);
      } catch (MalformedURLException e) {
         e.getStackTrace();
      }
      return parsedUrl.getProtocol();
   }

}
